import {
  Controller,
  Get,
  Post,
  Body,
  Patch,
  Param,
  Delete,
  Query,
  DefaultValuePipe,
  ParseIntPipe,
  UseGuards,
} from '@nestjs/common';
import { ArticleService } from 'src/service/article.service';
import { CreateArticleDto } from 'src/dto/create-article.dto';
import { UpdateArticleDto } from 'src/dto/update-article.dto';
import { QueryArticleDto } from 'src/dto/query-article.dto';
import { AdminGuard } from 'src/guards/admin.guard';
import { SitemapService } from 'src/service/sitemap.service';

@UseGuards(AdminGuard)
@Controller('admin/article')
export class ArticleController {
  constructor(
    private readonly articleService: ArticleService,
    private readonly sitemapService: SitemapService,
  ) {}

  @Post()
  async create(@Body() createArticleDto: CreateArticleDto) {
    let resut = await this.articleService.create(createArticleDto);
    try {
      await this.sitemapService.generateSitemap();
    } catch (error) {
      console.log('error', error);
    } finally {
      return resut;
    }
  }

  @Get()
  query(@Query() query: QueryArticleDto) {
    return this.articleService.query(query);
  }

  @Get(':id')
  findOne(@Param('id') id: string) {
    return this.articleService.findOne(+id);
  }

  @Patch(':id')
  async update(
    @Param('id') id: string,
    @Body() updateArticleDto: UpdateArticleDto,
  ) {
    let result = await this.articleService.update(+id, updateArticleDto);
    try {
      await this.sitemapService.generateSitemap();
    } catch (error) {
      console.log('error', error);
    }
    return result;
  }

  @Delete(':id')
  remove(@Param('id') id: string) {
    return this.articleService.remove(+id);
  }
}
